﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using MT.eVotePRS.Model;
using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Presenter;

namespace MT.eVotePRS.UI.UserControls
{
    public partial class CheckStudent : UserControl, IViewCheckStatus
    {
        #region IViewCheckStatus

        CheckingStatus _toCheck;
        Handsets _handset;
        StudentInfo _student;
        InventoryTxn _txn;

        public CheckingStatus ToCheck { get { return _toCheck;} }

        public Handsets Handset
        {
            get
            {
                return _handset;
            }
            set
            {
                _handset = value;
                lblHandset.Text = _handset.MAC;
            }
        }

        public StudentInfo Student
        {
            get
            {
                _student.StudentNo = txtStudentID.Text;
                return _student;
            }
            set
            {
                _student = value;
                lblShowBarcode.Text = _student.Barcode;
                lblStudentName.Text = _student.StudentName;
                lblIsGraduated.Text = _student.IsGraduate ? "Yes" : "No";
            }
        }

        public InventoryTxn Txn
        {
            set
            {
                _txn = value;
                lblDateTime.Text = _txn.TxnTime.ToShortDateString();
                lblStatus.Text = _txn.UserEvent;
            }
        }

        public event EventHandler IdEntered;

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public CheckStudent()
        {
            InitializeComponent();
            this.txtStudentID.KeyDown += OnKeyDown;
            this.txtStudentID.KeyUp += OnKeyUp;

            _toCheck = CheckingStatus.Student;
        }

        void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(txtStudentID.Text))
                {
                    RaiseIdEntered();
                }
            }
        }
        
        void OnKeyUp(object sender, KeyEventArgs e)
        {
            if (string.IsNullOrEmpty(txtStudentID.Text))
            {
                _student.StudentNo = txtStudentID.Text;
                RaisePropertyChanged("STUDENT");
            }
        }

        protected virtual void RaiseIdEntered()
        {
            EventHandler handlers = IdEntered;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handlers = PropertyChanged;
            if (handlers != null)
            {
                handlers(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        
    }
}
